package com.muge.cartoon.naruto.ui.activity;

import android.app.Activity;
import android.app.Notification;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.publicModule.model.notification.NotificationManage;
import com.android.publicModule.network.DownloadProgress;
import com.android.publicModule.system.SystemInfo;
import com.android.publicModule.tools.Tools;
import com.android.publicModule.webview.MugeWebView;
import com.android.publicModule.webview.OnLoadProgressListener;
import com.android.publicModule.webview.WebViewControl;
import com.android.publicModule.webview.WebViewManage;
import com.muge.cartoon.naruto.R;

public class MainActivity extends Activity {
	
	private WebViewManage mWebViewManage;
	private MugeWebView mWebView;
	private RelativeLayout mLinearLayout;
	private ImageView progressbar;
	/** Handler **/
	private Handler mHandler = null;
	private static final int ON_PAGE_STARTED = 1 ,ON_PROGRESS_CHANGED = 2 ,ON_PAGE_FINISHED = 3;
	private static int nid = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e("muge", "onCreate()");
								
        mLinearLayout = (RelativeLayout)this.findViewById(R.id.main);
        progressbar = (ImageView)this.findViewById(R.id.progressbar);
        
		mHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if (msg != null)
				{
					ViewGroup.LayoutParams para;
					switch(msg.what) {
					
						case ON_PAGE_STARTED:
					        para = progressbar.getLayoutParams();
					        para.height = 2;
					        para.width = 0;
					        progressbar.setLayoutParams(para);
							progressbar.setVisibility(View.VISIBLE);
							break;
						case ON_PROGRESS_CHANGED:
							 para = progressbar.getLayoutParams();
						     para.height = 2;
						     if(msg.arg1<100)
						     {
						    	 para.width = (int)(orientation == Configuration.ORIENTATION_PORTRAIT?SystemInfo.getScreenWidth(MainActivity.this):SystemInfo.getScreenHeight(MainActivity.this)*msg.arg1/100.0);
						     	progressbar.setLayoutParams(para);
							 	progressbar.setVisibility(View.VISIBLE);
						     }
						     else
						     {
						    	 para.width = 0;
							     progressbar.setLayoutParams(para);
								 progressbar.setVisibility(View.GONE);
						     }
							break;
						case ON_PAGE_FINISHED:
					        para = progressbar.getLayoutParams();
					        para.height = 2;
					        para.width = 0;
					        progressbar.setLayoutParams(para);
							progressbar.setVisibility(View.GONE);
							break;
						default:
							break;
					}
				}
			}
			
		};
		
        ///*
		mWebViewManage = new WebViewManage();
		mWebViewManage.setActivity(this);
        mWebView = (MugeWebView)this.findViewById(R.id.webview);
        mWebViewManage.setMugeWebView(mWebView);
        mWebViewManage.initDefaultWeb();
        mWebViewManage.setOnLoadProgressListener(new OnLoadProgressListener() {

			@Override
			public void onPageStarted(String url) {
				// TODO Auto-generated method stub
				mHandler.sendEmptyMessage(ON_PAGE_STARTED);
			}

			@Override
			public void onProgressChanged(int newProgress) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = ON_PROGRESS_CHANGED;
				msg.arg1 = newProgress;
				mHandler.sendMessage(msg);
			}

			@Override
			public void onPageFinished(String url) {
				// TODO Auto-generated method stub
				mHandler.sendEmptyMessage(ON_PAGE_FINISHED);
			}
        	
        });
        // */
        /*
        mWebViewManage = new WebViewManage(this);
        mWebViewManage.initDefaultWeb();
        mWebView = mWebViewManage.getWebView();
        mLinearLayout.addView(mWebView);
        */
        
        final NotificationManage mNotificationManage = new NotificationManage(this);
        
        String id="0001";
        String url="http://192.168.1.22/yy.apk";
        String savePath=Tools.sdcardPath+"yy-2013-10-29.apk";
        DownloadProgress progress = new DownloadProgress() {
        	
        	private Notification  mNotification;

			@Override
			public void downloadReady(String id, String url, Object downloadInfo) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void downloadStart(String id, String url, String savePath,
					boolean downloadPhone, long downloaded, long total,
					Object downloadInfo) {
				// TODO Auto-generated method stub
				int percent = (int)(downloaded*100.0/total);
				mNotification = mNotificationManage.createDownloadProgressNotification("正在下载。。。","正在下载YY！", percent);
				nid++;
				mNotificationManage.notify(nid, mNotification);
			}

			@Override
			public void downloading(String id, String url, String savePath,
					boolean downloadPhone, long downloaded, long total,
					Object downloadInfo) {
				// TODO Auto-generated method stub
				int percent = (int)(downloaded*100.0/total);
				mNotificationManage.setDownloadProgress(mNotification, percent);
				mNotificationManage.notify(nid, mNotification);
			}

			@Override
			public void downloadSucess(String id, String url, String savePath,
					boolean downloadPhone, long total, int type, String reason,
					Object downloadInfo) {
				// TODO Auto-generated method stub
				int percent = 100;
				if(mNotification==null)
				{
					mNotification = mNotificationManage.createDownloadProgressNotification("正在下载。。。","正在下载YY！", percent);
				}
				mNotificationManage.setDownloadProgress(mNotification, percent);
				mNotificationManage.notify(nid, mNotification);
			}

			@Override
			public void downloadFail(String id, String url, String savePath,
					boolean downloadPhone, int type, String reason,
					Object downloadInfo) {
				// TODO Auto-generated method stub
				
			}
        	
        };
        Object downloadInfo = null;
        
        //WebViewControl.getInstance(this).downloadApk(id, url, savePath, progress, downloadInfo);
        
        Notification  mNotification;
        mNotification = mNotificationManage.createNotificationNews("YY语音助理","你的不可多得的手机助手！");
		nid++;
		mNotificationManage.notify(nid, mNotification);

		Notification mNotification2 = mNotificationManage.createNotificationImg("火影忍者");
		nid++;
		mNotificationManage.notify(nid, mNotification2);
    }
    
    @Override
	public void onStart() {
		super.onStart();
		Log.e("muge", "onStart()");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.e("muge", "onResume()");
		//mWebViewManage.loadUrl("http://192.168.1.22/cartoon/naruto/644/index.html");//
		
		//file:///sdcard/feiyou/download/14.swf
		
		//http://192.168.1.22/flash/icon.png
		//http://192.168.1.22/cartoon/naruto/naruto-phone.html
		//http://192.168.1.22/fish
			//index.html
		mWebViewManage.loadUrl("http://192.168.1.22/cartoon/naruto/naruto-phone.html");//http://192.168.1.22/cartoon/naruto/naruto-phone.html
		mWebView.requestFocus();//http://192.168.1.22/flash/index.html   http://www.56.com/flashApp/swf_effect/f142.swf
		//"file:///sdcard/cartoon/naruto/644/index.html"
		
		//mWebViewManage.loadUrl("file:///sdcard/cartoon/naruto/644/index.html");//
		
		//mWebViewManage.loadUrl("file:///sdcard/cartoon/naruto/644/img/1.jpg");
		
		//mWebViewManage.loadUrl("http://192.168.22.22/cartoon/naruto/644/img.html");
		
		//testLoadImg();
	}
	
	private Thread loadNextImgThread;
	private void testLoadImg() {
		if(loadNextImgThread==null)
		{
			loadNextImgThread = new Thread() {
				public void run() {
					
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
					}
					
					mWebViewManage.loadJavaScript("javascript:setClient(1,'webview')");
					
					mWebViewManage.loadJavaScript("javascript:showImg()");
					
					loadNextImgThread = null;
				}
			};
			try {
				loadNextImgThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	@Override
    protected void onPause() {
        super.onPause();
        Log.e("muge", "onPause()");
    }
	
	@Override
	public void onStop() {
		super.onStop();
		Log.e("muge", "onStop()");
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.e("muge", "onKeyDown() keyCode="+keyCode);
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)// 按下返回键
        {
            finish();
            return false;
        }
		return true;
	}
	
	@Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("muge", "onDestroy()");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e("muge", "onSaveInstanceState()");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("muge", "onRestoreInstanceState()");
    }

    private int orientation = Configuration.ORIENTATION_PORTRAIT;
    // 需在AndroidManifest里注册Activity的configChanges属性
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.e("muge", "onConfigurationChanged()");
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        	orientation = Configuration.ORIENTATION_LANDSCAPE;
            Log.e("muge", "当前屏幕切换成横屏显示");
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        	orientation = Configuration.ORIENTATION_PORTRAIT;
            Log.e("muge", "当前屏幕切换成竖屏显示");
        }
    }
	
	@Override
	public void finish() {
		super.finish();
		Log.e("muge", "finish()");
	}
    
}